﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace CPPEI.WebApi
{
    class InstallProcess
    {
        public static void ProcessRequest(HttpContext context)
        {
            context.Response.Write("<html>");
            context.Response.Write("<body>");

            string toolPath = context.Server.MapPath("~/bin/CPPEI.WebApiTool.exe");
            string toolDir = context.Server.MapPath("~/bin/");

            context.Response.Write(string.Format("run:{0}<br/>", toolPath));

            try
            {
                Process p = new Process();
                p.StartInfo.UseShellExecute = true;
                p.StartInfo.RedirectStandardOutput = false;
                p.StartInfo.FileName = toolPath;
                //p.StartInfo.WorkingDirectory = toolDir;
                //p.StartInfo.CreateNoWindow = false;
                //p.StartInfo.Verb = "RunAs";
                if (p.Start())
                {
                    //string output = p.StandardOutput.ReadToEnd();
                    //context.Response.Write(string.Format("{0}<br/>", output));
                    //context.Response.Write(string.Format("{0}<br/>", "运行程序中"));
                    //p.WaitForExit();
                    context.Response.Write(string.Format("{0}<br/>", "run success"));
                }
                else
                {
                    context.Response.Write(string.Format("{0}<br/>", "run failure"));
                }
            }
            catch(Exception ex)
            {
                context.Response.Write(string.Format("{0}<br/>", "run error"));
                context.Response.Write(ex.ToString());
            }

            context.Response.Write("</body>");
            context.Response.Write("</html>");
            //string output = p.StandardOutput.ReadLine();
            //while (output != null)
            //{
            //    context.Response.Write(string.Format("{0}<br/>", output));
            //    output = p.StandardOutput.ReadLine();
            //}

        }
    }
}
